#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "mysql.h"
#include "myvideo.h"
#include "libavformat/avformat.h"
#include "libavcodec/opt.h"

static AVCodec **input_codecs = NULL;
static int nb_input_codecs = 0;
static char  *audio_codec_name = NULL;
static char *video_codec_name = NULL;
AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
AVFormatContext *avformat_opts;
MYVIDEOINFO *myvideoinfo;

//set filename md5 code and path
int get_video_base_info(char *filepath) {


	AVFormatContext *ic;
	int ret = av_open_input_file(&ic, filepath, NULL, 0, NULL);
	if(ret) return 0;

	av_find_stream_info(ic);

	int ret1 = set_video_base_info(filepath);
	if(!ret1) return ret1;

	int i = 0;

    for(i=0;i<ic->nb_streams && i < 6;i++) {

        AVStream *st = ic->streams[i];
        AVCodecContext *dec = st->codec;

        avcodec_thread_init(dec, 1);
    	AVCodec *avcode = avcodec_find_decoder(dec->codec_id);

    	set_video_base_stream(avcode->long_name);

        switch (dec->codec_type) {
        case AVMEDIA_TYPE_AUDIO:
        	set_video_base_acode(avcode->name);
            break;
        case AVMEDIA_TYPE_VIDEO:
        	set_video_base_resolution(dec->width, dec->height);
        	set_video_base_vcode(avcode->name);
        	set_video_base_fps(ic->streams[i]->r_frame_rate.num);
            break;
        case AVMEDIA_TYPE_DATA:
            break;
        case AVMEDIA_TYPE_SUBTITLE:
        case AVMEDIA_TYPE_ATTACHMENT:
        case AVMEDIA_TYPE_UNKNOWN:
            break;
        default:
            abort();
        }
    }


	set_video_base_streamnum(ic->nb_streams);
	set_video_base_vsize(ic->file_size);
	set_video_base_bitrate(ic->bit_rate);
	set_video_base_vtime(ic->duration);

	myvideoinfo = myvideoinfo;
	return 1;
}


int set_video_base_info(char *filepath) {


	myvideoinfo = malloc(sizeof(MYVIDEOINFO));
	memset(myvideoinfo, 0, sizeof(MYVIDEOINFO));

	myvideoinfo->filename = filepath;
	char *md5 = malloc(33*sizeof(char));
	memset(md5, 0, 33);

	int i = 0;

	i = CalcFileMD5(filepath, md5);

	if(i == 0) return 0;

	myvideoinfo->md5 = md5;
	myvideoinfo->path = filepath;

	return 1;
}

int set_video_base_streamnum(int num) {

	myvideoinfo->streamnum = num;
}


int set_video_base_vsize(long long size) {

	myvideoinfo->vsize = size;
}

int set_video_base_vtime(long vtime) {

	vtime = vtime / 100000;
	vtime = (vtime + 5)/10;

	myvideoinfo->vtime = vtime;
}

int set_video_base_fps(int fps) {

	myvideoinfo->fps = fps;
}

int set_video_base_resolution(int width, int height) {

	myvideoinfo->width = width;
	myvideoinfo->height = height;
}

int set_video_base_bitrate(int bitrate) {

	myvideoinfo->bitrate = bitrate;
}

void set_video_base_vcode(char *vcode) {

	myvideoinfo->vcode = vcode;
}

void set_video_base_acode(char *acode) {

	myvideoinfo->acode = acode;
}

void set_video_base_stream(char *stream) {

	int len = 0;
	len = strlen(stream);

	char *tmpstream = malloc(301*sizeof(char));

	memset(tmpstream, 0, 301);

	if(len > 300)
		strncpy(tmpstream, stream, 300);
	else
		strcpy(tmpstream, stream);

	int i;
	for(i = 0; i < sizeof(myvideoinfo->stream); i++) {

		if(!myvideoinfo->stream[i]) {

			myvideoinfo->stream[i] = tmpstream;
			break;
		}
	}
	return;
}

void save_video_info() {

//first  delete record from table v_baseinfo and code vid is md5;

	char *sql = malloc(2048*sizeof(char));

	memset(sql, 0, 2048);
	sprintf(sql, "delete from v_baseinfo where vid ='%s'", myvideoinfo->md5);

	executeUpdate(sql);
	memset(sql, 0, 2048);
	sprintf(sql, "insert into v_baseinfo(vid, vformat, vsize, vtime, streamnum, stream1, stream2, stream3, stream4, stream5, stream6, fps, width, height, bitrate, vcode, acode, ctime ) values('%s', '', %ld, %ld, %d, '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s', now\(\))",
			myvideoinfo->md5,  myvideoinfo->vsize, myvideoinfo->vtime, myvideoinfo->streamnum, myvideoinfo->stream[0], myvideoinfo->stream[1], myvideoinfo->stream[2], myvideoinfo->stream[3],
			myvideoinfo->stream[4], myvideoinfo->stream[5], myvideoinfo->fps, myvideoinfo->width, myvideoinfo->height, myvideoinfo->bitrate,myvideoinfo->vcode, myvideoinfo->acode);
	executeUpdate(sql);
}

int main(void) {


	char *filename = "/var/video/X-Men.First.Class.2011.BluRay.720p.DTS.x264-CHD.mkv";

    int64_t ti;

    av_log_set_flags(AV_LOG_SKIP_REPEATED);

    avcodec_register_all();
#if CONFIG_AVDEVICE
    avdevice_register_all();
#endif
#if CONFIG_AVFILTER
    avfilter_register_all();
#endif
    av_register_all();

#if HAVE_ISATTY
    if(isatty(STDIN_FILENO))
        url_set_interrupt_cb(decode_interrupt_cb);
#endif

    int ret = get_video_base_info(filename);
    if(ret)
    	save_video_info();
}
